package jianzhi; /**
 * description:
 * author:张腾
 * date:2021-06-30
 */

/**
 * 假设把某股票的价格按照时间先后顺序存储在数组中，请问买卖该股票一次可能获得的最大利润是多少？
 */
public class jianzhi63 {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int max = 0;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (prices[i]>=prices[j] && prices[i]-prices[j]>max)
                    max = prices[i]-prices[j];
            }
        }
        return max;
    }
    //第二种解法
    public int maxProfit2(int[] prices) {
        int cost = Integer.MAX_VALUE , profit = 0;
        for (int price : prices) {
            cost = Math.min(cost,price);
            profit = Math.max(profit,price-cost);
        }
        return profit;
    }
}
